
from backend.table.Table6 import Table6
from backend.table.Table7 import Table7


def calculate_voltage(object_type, place_type, multiple_num, voltage_type, peak_voltage):

    # 定位行
    if peak_voltage < 60:
        row = 0
    elif peak_voltage < 71:
        row = 1
    elif peak_voltage < 184:
        row = 2
    elif peak_voltage < 212:
        row = 3
    elif peak_voltage < 354:
        row = 4
    elif peak_voltage < 848:
        row = 5
    elif peak_voltage < 1414:
        row = 6
    elif peak_voltage < 10000:
        row = 7
    elif peak_voltage < 14140:
        row = 8
    else:
        row = 9
    if voltage_type == '1':
        if voltage_type < 42.4:
            row = 0
        if 42.4 < voltage_type <= 71:
            row = 1

    # 定位列
    if (object_type, place_type, multiple_num) == (0,0,0):
        column = 0
    if (object_type, place_type, multiple_num) == (0,0,1):
        column = 1
    if (object_type, place_type, multiple_num) == (0,1,0):
        column = 2
    if (object_type, place_type, multiple_num) == (0,1,1):
        column = 3
    if (object_type, place_type, multiple_num) == (1,0,0):
        column = 4
    if (object_type, place_type, multiple_num) == (1,0,1):
        column = 5
    if (object_type, place_type, multiple_num) == (1,1,0):
        column = 6
    if (object_type, place_type, multiple_num) == (1,1,1):
        column = 7
    
    result = Table6[row][column]

    if type(result) == int:
        return result
    elif type(result) == tuple:
        return result[0] *peak_voltage + result[1]
    if type(result) == str and result != "见表7":
        return result
    else:
        result = Refer_table7(peak_voltage, multiple_num)

    return result


def Refer_table7(peak_voltage, multiple_num):
    for i in range(1, len(Table7)):
        if peak_voltage > Table7[i-1][0] and peak_voltage <= Table7[i][0]:
            return Table7[i][multiple_num +1]
    return "Func Refer_table7 Err"            


if __name__ == "__main__":
    v = calculate_voltage(object_type=0, place_type=1, multiple_num=1, voltage_type=0, peak_voltage=62)
    print(v)
